import createScroll from './createScroll';

export default ComposeComponent => class extends ComposeComponent {
        getScroll = () => {
            const { scroller } = this.props;

            if (!scroller) {
                return WPT.scroll;
            }

            return createScroll($(this.wall).parents(scroller));
        };

        initLoaderTimer() {
            // 图墙容器
            const state = this.state;
            this.scrollKey = `goodswall${this.state.key}${this.state.time}`;
            this.scroll = this.getScroll();

            if ($.isEmptyObject(state.firstScreenData)) {
                this.fetch();
            } else {
                this.ajaxSuccBack && this.ajaxSuccBack.call(this, state.firstScreenData);
                this.state.firstScreenData = null;
            }

            this.show();

            if (!WPT[this.state.key]) {
                WPT[this.state.key] = {};
            }
            if (!WPT[this.state.key].show) {
                WPT[this.state.key].show = {};
            }
            if (!WPT[this.state.key].hide) {
                WPT[this.state.key].hide = {};
            }
            WPT[this.state.key].show[this.state.time] = () => {
                this.show();
            };

            WPT[this.state.key].hide[this.state.time] = () => {
                this.hide();
            };
        }

        show() {
            this.scroll.appendScroll(this.scrollKey, this.refresh);
        }

        hide() {
            this.scroll.clear(this.scrollKey);
        }

        forceRerender(reConfig) {
            this.state = $.extend(this.state, reConfig);
            this._isLoading = false;
            this.state.isEnd = false;
            this.state.data = [];
            WPT.$curPage
                .find('.wall-container')
                .find('.wall-col')
                .html('');
            if (!this.state.firstScreenData) {
                this.fetch();
            } else {
                this.ajaxSuccBack && this.ajaxSuccBack.call(this, this.state.firstScreenData);
                this.state.firstScreenData = null;
            }
        }

        refresh = (scrollTop, scrollHeight, containerHeight) => {
            // scrollTop 滚动高度  scrollHeight 整个滚动条高度
            if (this.state.isEnd || this._isLoading || !this._updated) return;
            scrollTop = scrollTop || document.documentElement.scrollTop
            || window.pageYOffset || document.body.scrollTop;
            // 判断滚动条高度，确定是否要加载下一屏
            const wHeight = containerHeight || document.documentElement.clientHeight;
            const dHeight = scrollHeight || document.body.scrollHeight; // document.documentElement.offsetHeight 不要了
            if (dHeight - (scrollTop + wHeight) < this.state.bottomHeight) {
                this.fetch();
            }
        };

        // 发异步请求获取数据
        fetch() {
            this._isLoading = true;
            this._updated = false;

            // 请求url
            const url = this.state.ajaxUrl;
            const data = $.extend(
                { start: this.state.start, page: this.state.page },
                this.state.serverData,
                this.state.ajaxParam
            );
            let ajax = WPT.getData;
            if (this.state.ajaxType.toLowerCase() == 'post') {
                ajax = WPT.postData;
            }
            ajax(url, data, res => {
                this._isLoading = false;
                this.ajaxSuccBack && this.ajaxSuccBack.call(this, res);
            });
        }

        destroy() {
            if (WPT[this.state.key] && WPT[this.state.key].hide) {
                delete WPT[this.state.key].hide[this.state.time];
            }
            if (WPT[this.state.key] && WPT[this.state.key].show) {
                delete WPT[this.state.key].show[this.state.time];
            }

            WPT.scroll.clear(`goodswall${this.state.key}${this.state.time}`);
        }
};
